package com.tuhu.server.db.op;

import com.alibaba.fastjson.util.IOUtils;
import com.tuhu.server.db.DBHelper;
import com.tuhu.server.db.SqlResult;
import com.tuhu.server.db.bean.ActivateRecordBean;

import java.sql.ResultSet;
import java.sql.SQLException;

public class ActivateRecordDB {

    public static ActivateRecordBean find(String active_code) {

        String sql = String.format("SELECT * FROM activate_record WHERE active_code = '%s'", active_code);

        SqlResult sr = DBHelper.executeQueryEx(sql);

        try {
            if (sr == null) {
                return null;
            }

            ResultSet rs = sr.rs;
            if (rs.next()) {
                ActivateRecordBean bean = new ActivateRecordBean();
                bean.id = rs.getInt("id");
                bean.active_code_type = rs.getInt("active_code_type");
                bean.active_code = rs.getString("active_code");
                bean.device_id = rs.getString("device_id");
                bean.brand = rs.getString("brand");
                bean.model = rs.getString("model");
                bean.use_count = rs.getInt("use_count");
                bean.create_time = rs.getString("create_time");
                bean.activate_time = rs.getString("activate_time");
                bean.vip_end_time = rs.getString("vip_end_time");
                bean.last_use_time = rs.getString("last_use_time");
                return bean;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            IOUtils.close(sr);
        }

        return null;
    }

    public static boolean activate(String active_code, String device_id, String brand, String model,
                                   String activate_time, String vip_end_time) {
        String sql = String.format("UPDATE activate_record SET device_id = '%S', brand = '%S', model ='%s'," +
                " activate_time = '%s', vip_end_time = '%s' " +
                " WHERE active_code = '%s'", device_id, brand, model, activate_time, vip_end_time, active_code);

        return DBHelper.executeUpdate(sql) > 0;
    }

    public static boolean mockUpdate(String active_code, String last_use_time) {
        String sql = String.format("UPDATE activate_record SET use_count = use_count + 1, last_use_time = '%s'" +
                " WHERE active_code = '%s'", last_use_time, active_code);

        return DBHelper.executeUpdate(sql) > 0;
    }

    public static boolean insert(String active_code, int active_code_type, String creat_time) {
        String sql = String.format("INSERT INTO activate_record(active_code_type, active_code, create_time) " +
                " VALUES(%d, '%s', '%s')", active_code_type, active_code, creat_time);

        return DBHelper.executeInsert(sql) > 0;
    }

    public static boolean unbind(String active_code) {
        String sql = String.format("UPDATE activate_record SET device_id = null, brand = null, model = null," +
                " activate_time = null " +
                " WHERE active_code = '%s'", active_code);

        return DBHelper.executeUpdate(sql) > 0;
    }


}
